<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Category;
use App\Models\Goods;
use App\Http\Requests\Admin\AdminCategory\CategoryRequest;
use App\Models\CategoryBrand;
use Illuminate\Support\Facades\Input;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Commands\OperationLogCommand;

class CategoryController extends Controller
{


    public function category_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * 分类列表
     * 
     * param 
     *
     * @date 2015/12/10
     * @author Leen
     */
    public function index(Request $request)
    {
        $this->category_gate();

        $categoryInfo = Category::getTreeList();

        return view('admin.category.index',compact(['categoryInfo']));

    }//end func index()

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Category $category)
    {

        $this->category_gate();

        return view('admin.category.create',compact(['category']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryRequest $categoryRequest)
    {

        $this->category_gate();

        $category =Category::create($categoryRequest->all());
        $this->dispatch(
            new OperationLogCommand('添加分类','Category','cat_id:'.$category->cat_id)
        );

        return Redirect('admin/category');

    }//end func store()

    /**
     * 获取分类列表
     *
     * param
     *
     * @date 2016/04/19
     * @author Leen
     */
    public function show($id)
    {

        $this->category_gate();

        $categoryInfo = Category::getTreeList();

        return $categoryInfo;
    }//end func show()

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $this->category_gate();

        $categoryInfo = Category::find($id);
        return view('admin.category.edit',compact('categoryInfo'));
    }//end func edit()

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($id, CategoryRequest $categoryRequest)
    {
        $this->category_gate();

        $categoryInfo = Category::find($id);
        $categoryInfo->update($categoryRequest->all());
        $this->dispatch(
            new OperationLogCommand('修改分类','Category','cat_id:'.$id)
        );
        return Redirect('admin/category');

    }//end func update()

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->category_gate();

        $return = ['status'=>1, 'message'=>'', 'data'=>''];
        $isHasChildren = Category::where('parent_id', $id)->get();

        if(!empty($isHasChildren->count()) || $isHasChildren->count()!=0){

            $return['status'] = 0;
            $return['message'] = '分类存在子节点不能删除';
            return Response()->json($return);

        }//end if()

        //分类下有商品不能删除
        $isHasGoods = Goods::where('cat_id', $id)->get();

        if(!empty($isHasGoods->count()) || $isHasGoods->count()!=0){

            $return['status'] = 0;
            $return['message'] = '分类下有商品不能删除';

            return Response()->json($return);

        }//end if()


        $category = Category::find($id);

        $category->delete();
        $this->dispatch(
            new OperationLogCommand('删除分类','Category','cat_id:'.$id)
        );

        //return Redirect('admin/category');
        return Response()->json($return);

    }//end func destory()


    public function sort(Request $request){

        $this->category_gate();

        foreach ($request->get('list_order') as $id=>$v){

            Category::find($id)->update(['sort_order'=>$v]);
        }
        return Redirect('admin/category');

    }//end func sort()

    public function toggleShow(){

        $this->category_gate();

        $id = Input::get("id");
        $is_recommend = Category::where("cat_id",$id)->pluck("is_recommend");
        $data = array();
        $category = Category::find($id);

        if($is_recommend==0){
            $data['is_recommend'] = 1;
            $data['cat_id'] = $id;
            if($category->hasChildren()){
                $data['childIds']=$category->getChildIds();
                foreach($category->getChildIds() as $key=>$childId){
                    Category::where("cat_id",$childId)->update(['is_recommend'=>1]);
                }
                $res = Category::where("cat_id",$id)->update(['is_recommend'=>1]);
            }else{
                $res = Category::where("cat_id",$id)->update(['is_recommend'=>1]);
            }

        }else{
            $data['is_recommend'] = 0;
            $data['cat_id'] = $id;
            if($category->hasChildren()){
                $data['childIds']=$category->getChildIds();
                foreach($category->getChildIds() as $key=>$childId){
                    Category::where("cat_id",$childId)->update(['is_recommend'=>0]);
                }
                $res = Category::where("cat_id",$id)->update(['is_recommend'=>0]);
            }else{

                $res = Category::where("cat_id",$id)->update(['is_recommend'=>0]);
            }

        }
        if($res != false){
            //return $data;
            $data['info'] = true;
        }else{
            $data['info'] = false;
        }
        return $data;

    }


}//end class CategoryController
